package mww.servlet.teacher;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import mww.common.DaoException;
import mww.common.ServiceException;
import mww.javabean.service.TeacherCService;
import mww.javabean.service.TeacherCServiceImpl;
import mww.javabean.service.TeacherService;
import mww.javabean.service.TeacherServiceImpl;
public class DelTeacherServlet extends HttpServlet {
	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}
	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		TeacherService teacherService = new TeacherServiceImpl();
		TeacherCService tcService = new TeacherCServiceImpl();
		HttpSession session=request.getSession();
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		String teacherNo=request.getParameter("teacherNo");
		try{
			//删除教师信息前判断从表中是否存在关联记录，若不存在关联进行删除，否则禁止删除
			if(tcService.checkTeacherCExistByTeacherNo(teacherNo)){
				session.setAttribute("message", "所要删除记录存在关联项，禁止删除教师信息！");
				request.getRequestDispatcher("/admin/listAllTeacherServlet").forward(request, response);
			}else{
		  		teacherService.delTeacher(teacherNo);
		  		session.setAttribute("message", "删除教师信息成功！");
		  		request.getRequestDispatcher("/admin/listAllTeacherServlet").forward(request, response);
			}
		}catch (ServiceException e) {
			e.printStackTrace();
			request.setAttribute("se", e);
			request.getRequestDispatcher("/serviceException.jsp").forward(request, response);
		} catch (DaoException e) {
			e.printStackTrace();
			request.setAttribute("de", e);
			request.getRequestDispatcher("/daoException.jsp").forward(request, response);
		}
	}
}
